package com.loginlib.table.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loginlib.R;
import com.loginlib.table.TableBillActivity;
import com.loginlib.table.TableNumActivity;
import com.wanhe.base.adapter.TBaseAdapter;
import com.wanhe.base.adapter.ViewHolderUtil;
import com.wanhe.base.model.base.Table;
import com.wanhe.base.model.holder.TableInfoHolder;
import com.wanhe.base.util.TimeUtil;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;


/**
 * Created by Administrator on 2016/7/25.
 */
public class TableGridViewAdapter extends TBaseAdapter<Table> {
    public TableGridViewAdapter(Context context) {
        super(context, R.layout.table_item_layout);
    }

    @Override
    protected void fillData(ViewHolderUtil viewHolderUtil, int position, final Table table) {
        setValue(viewHolderUtil, table);
        viewHolderUtil.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                jumpTableNum(table);
                return false;
            }
        });
        viewHolderUtil.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (table.getTableOpened() == Table.EMPTY) {
                    jumpTableNum(table);
                } else if (table.getTableOpened() == Table.OPEN) {
                    //获取默认的开台人数
                    TableInfoHolder.getInstance().setCustomNum(table.getTablePpl());
                    jumpTableBill(table);
//                    toCuiYuBill();
                } else if (table.getTableOpened() == Table.ERROR) {
                    return;
                }
            }
        });
    }

    public void setValue(ViewHolderUtil viewHolderUtil, Table table) {
        viewHolderUtil.setText(R.id.tv_table_name, table.getName());
        TextView tvTotalPrice = viewHolderUtil.getView(R.id.tv_total_price);
        TextView tvOpenTableTime = viewHolderUtil.getView(R.id.tv_open_table_time);
        TextView tvCustomNumber = viewHolderUtil.getView(R.id.tv_custom_number);
        TextView tvIsVIP = viewHolderUtil.getView(R.id.tv_isVIP);
        RelativeLayout rlBottomLayout = viewHolderUtil.getView(R.id.rl_bottom_layout);
        // TODO: 2016/7/26 餐桌消费价格
        int isOpen = table.getTableOpened();
        switch (isOpen) {
            // TODO: 2016/7/26 设置背景颜色
            case Table.EMPTY:
                Log.d("Table", "Table.EMPTY");
                rlBottomLayout.setVisibility(GONE);
                rlBottomLayout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.table_bg_empty_gray));
                break;
            case Table.ERROR:
                // TODO: 2016/7/26 错误颜色
                break;
            case Table.OPEN:
//            isOpened = true;
                rlBottomLayout.setVisibility(VISIBLE);
                rlBottomLayout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.table_bg_normal_green));
                tvCustomNumber.setText(table.getTablePpl() + "人");

                int time = Integer.valueOf(table.getTableOpenTime());
                String timer = TimeUtil.secToTime(time);
                tvOpenTableTime.setText(timer);
                tvTotalPrice.setText(table.getBillAmount() + "元");//设置总消费
                break;
        }
    }

    //人数界面
    private void jumpTableNum(Table mTable) {
        TableInfoHolder.getInstance().setTableName(mTable.getName());
        TableInfoHolder.getInstance().setTableNum(mTable.getID());

        Intent intent = new Intent(mContext, TableNumActivity.class);
        intent.putExtra(TableNumActivity.TABLE_NAME, mTable.getID());
        intent.putExtra(TableNumActivity.TABLE_BILLAMOUT, mTable.getBillAmount());
        intent.putExtra(TableNumActivity.TABLE_UUID, mTable.getUuid());
        intent.putParcelableArrayListExtra(TableNumActivity.TABLE_DATA, mTable.getStates());
        mContext.startActivity(intent);

    }

    //已选菜品界面
    private void jumpTableBill(Table mTable) {
        TableInfoHolder.getInstance().setTableName(mTable.getName());
        TableInfoHolder.getInstance().setTableNum(mTable.getID());
        Intent intent = new Intent(mContext, TableBillActivity.class);
        intent.putExtra(TableBillActivity.TABLEID, mTable.getID());//桌号
        intent.putExtra(TableBillActivity.TABLEID_UUID, mTable.getUuid());//uudi
        intent.putExtra(TableBillActivity.TABLE_BILLAMOUNT, mTable.getBillAmount());//消费总额
        intent.putParcelableArrayListExtra(TableBillActivity.TABLESTATE, mTable.getStates());
        mContext.startActivity(intent);
    }
}
